package com.dodayum.controller;

import com.dodayum.pojo.ServiceInformation;
import com.dodayum.service.ServiceInformationService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/service")
public class ServiceInformationController {

    @Resource
    ServiceInformationService service;

//    @RequestMapping("/all")
//    public String queryAll(Model model){
//        List<ServiceInformation> all = service.queryAll();
//        model.addAttribute("list",all);
//        return "all";
//    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "addService";
    }

    @RequestMapping("/add")
    public String add(ServiceInformation serviceInformation){
        System.out.println(serviceInformation.toString());
        service.insert(serviceInformation);
        return "redirect:/service/all";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(int serviceId,Model model){
        model.addAttribute("service",service.queryById(serviceId));
        return "update";
    }

    @RequestMapping("/updateService")
    public String updateService(ServiceInformation serviceInformation){
        service.updateById(serviceInformation);
        return "redirect:/service/all";
    }

    @RequestMapping("delete")
    public String delete(Integer serviceId,Model model){
        if (!ObjectUtils.isEmpty(serviceId)) {
            service.delete(serviceId);
        }
        List<ServiceInformation> all = service.queryAll();
        model.addAttribute("list",all);
        return "all";
    }

    @RequestMapping("queryServiceByName")
    public String queryServiceByName(String serviceName,Model model){
        List<ServiceInformation> list = service.queryServiceByServiceName(serviceName);
        model.addAttribute("list",list);
        return "all";
    }


}
